package com.example.webserver.service.impl;

import com.example.webserver.entity.Article;
import com.example.webserver.mapper.Articlemapper;
import com.example.webserver.service.Articleservice;
import com.example.webserver.entity.PageBean;
import com.example.webserver.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class Aritcleserviceimpl implements Articleservice {
    @Autowired
    Articlemapper  articlemapper;
    @Override
    public void add(Article article) {
        Map<String,Object>map= ThreadLocalUtil.get();
        int id= (int) map.get("id");
        article.setCreate_user(id);
        articlemapper.Insert(article);
    }

    @Override
    public void update(Article article) {
        articlemapper.update(article);
    }

    @Override
    public void delete(Integer id) {
        articlemapper.delete(id);
    }

    @Override
    public PageBean<Article> list(Integer cateqoryId, String state, Integer pagenum, Integer pagesize) {
        PageBean<Article> pb=new PageBean<>();
        PageHelper.startPage(pagenum,pagesize);

        System.out.println("1");
        Map<String,Object> map=ThreadLocalUtil.get();
        Integer id= (Integer) map.get("id");
        List<Article> rs= articlemapper.list(id,cateqoryId,state);
        Page<Article> p=(Page<Article>) rs;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }
}
